<div class="bjui-pageContent">
    <form id="protocoldictionary-edit" class="form-horizontal" method="post"  action='/protocoldictionary/edit'>
        <input type="hidden"  class="form-control input-nm" name="id" id="proto-id" value="{{id}}"  >
        <div class="form-group">
            <label class="col-lg-12 col-sm-12 control-label"><span class="required-span">*</span>传感器名称:
                 
            </label>
            <div class="col-lg-12 col-sm-12">
                <input type="text" class="form-control input-nm"  name="name" id="proto-name">
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-12 col-sm-12 control-label"><span class="required-span">*</span>传感器编号:
                 
            </label>
            <div class="col-lg-12 col-sm-12">
                <input type="text" class="form-control input-nm"  name="k" id="proto-k">
            </div>
        </div>
        
        <div class="form-group">
            <label class="col-lg-12 col-sm-12 control-label"><span class="required-span">*</span>数据类型:
            </label>
            <div class="col-lg-12 col-sm-12">
                <select class="selectpicker show-tick form-control input-nm"  name="t" id="proto-t">
                    <option value="" disabled selected  style = 'display：none;' >请选择传感器类型</option>
                    <option value="0">字节</option>
                    <option value="1">整型</option>
                    <option value="2">浮点型</option>
                    <option value="3">字符串</option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-12 col-sm-12 control-label">私有协议字典描述:
            </label>
            <div class="col-lg-12 col-sm-12">
                 <textarea style="width: 100%;height: 100px;"  name="memo" id="proto-memo"></textarea>
            </div>
        </div>
       
    </form>
</div>
<div class="btns-group bjui-pageFooter">
    <button type="button" class="close-btn">取消</button>
    <button type="submit" class="save-btn">保存</button>
</div>

<script type="text/javascript">

    $(document).ready(function() {
           var id=$("#proto-id").val(),protoNumberRegexp;
            $('#proto-t').selectpicker();
           
            if(id){
                getKtv(id);
            }
            dialog_close(); 
            if('{{roleinfo.role}}'=='管理员'){
                protoNumberRegexp={
                        validators: {
                            notEmpty: {
                                message: '请填写传感器编号'
                            },
                            regexp: {
                                regexp: /^([1-9]|[1-6][0-9]|70|15[1-9]|1[6-9][0-9]|2[0-4][0-9]|25[0-5])$/,
                                message: '请填写1至70、151至255之间的编号'
                            }
                        }
                    }
                $('#proto-k').attr('placeholder','请填写1至70、151至255之间的编号');
            }else{
                protoNumberRegexp={
                        validators: {
                            notEmpty: {
                                message: '请填写传感器编号'
                            },
                            between: {
                                min: 71,
                                max: 150,
                                message: '请填写71至150之间的编号'
                            }
                        }
                    }
                    $('#proto-k').attr('placeholder','请填写71至150之间的编号');
            }
            $('#protocoldictionary-edit').find('[name="t"]') .selectpicker() .change(function(e) { /* Revalidate the language when it is changed */ 
            $('#protocoldictionary-edit').formValidation('revalidateField', 't'); 
              }) .end().formValidation({       
                framework: 'bootstrap', 
                excluded: ':disabled',
                trigger:'blur', 
                icon: { valid: 'glyphicon glyphicon-ok', 
                invalid: 'glyphicon glyphicon-remove', 
                validating: 'glyphicon glyphicon-refresh' }, 
                fields: {                                 
                    name: {                            //字段
                        validators: {                     //验证器
                            notEmpty: {
                                message: '请填写传感器名称'
                            },
                            regexp: {
                                regexp: /^[\u4e00-\u9fa5_a-zA-Z0-9-]{2,30}$/,
                                message: '传感器名称只能由字母、数字、中文、下划线和减号组成,长度必须2到30之间'
                            }
                        }
                    },
                        k: protoNumberRegexp,
                    t:{
                       validators:{
                            notEmpty:{
                               message:'请填写传感器类型'
                            }
                        }
                    },
                    memo:{
                        validators: {
                            stringLength:{
                              message:'私有协议字典描述不得超过200个字符',
                              max:200
                            }
                        }
                    } 
                }
            })
             .on('success.form.fv', function(e) {
            // Prevent form submission
            e.preventDefault();

            // Get the form instance
            var $form = $(e.target);

            // Get the FormValidation instance
            var bv = $form.data('formValidation');
            // Use Ajax to submit form data
            $.post($form.attr('action'), $form.serialize(), function(result) {
                if(result.statusCode=='200'){
                    BJUI.alertmsg('ok',result.message);
                    BJUI.dialog('closeCurrent');
                    BJUI.navtab('refresh', 'sysprotocoldictionary');
                    // BJUI.navtab('refresh', 'sysprotocol')
                }else{
                  BJUI.alertmsg('error',result.message);
                }
            }, 'json');
        });
    });

function getKtv(id){
    $.ajax({
        url:'/protocoldictionary/getktv',
        type:'post',
        data:{'id':id},
        dataType:'json',
        success:function(json){ 
            $("#proto-name").val(json.data.name);
            $("#proto-k").val(json.data.k);
            $("#proto-t").val(json.data.t); 
            $('#proto-t').selectpicker('render');
            $("#proto-memo").val(json.data.memo);

        }
    })
}
    
</script>